package com.group02.community.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.group02.community.entity.community.ZyCommunity;
import com.group02.community.entity.community.ZyRoom;
import com.group02.community.entity.community.ZyUnit;
import com.group02.community.entity.dict.SysDictData;
import com.group02.community.entity.vo.RoomStatusVO;
import com.group02.community.entity.vo.queryVO.UnitQueryVO;
import com.group02.community.service.RoomService;
import com.group02.community.service.SysDictDataService;
import com.group02.community.utils.EasyExcelUtil;
import com.group02.community.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@RestController
@CrossOrigin
@Api
@RequestMapping("/room")
public class RoomController {
    @Autowired
    private RoomService roomService;


    @ApiOperation("获取房屋列表")
    @PostMapping("/pageList")
    public ResponseResult<IPage<ZyRoom>> getRoomPageList(Integer currentPage, Integer pageSize,@RequestBody UnitQueryVO unitQueryVO) {
        IPage<ZyRoom> roomPageList = roomService.getRoomPageList(currentPage, pageSize, unitQueryVO);
        return ResponseResult.ok(roomPageList);

    }

    @ApiOperation("获取所有的房屋状态")
    @GetMapping("/getRoomStatusList")
    public ResponseResult<List<RoomStatusVO>> getRoomStatusList() {

        return ResponseResult.ok(roomService.getRoomStatusList());
    }

    @ApiOperation("根据数组批量删除房间")
    @PostMapping("/delByIds")
    public ResponseResult<String> deleteCommunityByIds(@RequestBody String[] ids) {

        ArrayList<String> list = new ArrayList<>(Arrays.asList(ids));
        roomService.removeBatchByIds(list);
        return ResponseResult.ok("删除成功");
    }

    @ApiOperation("根据选中的数组导出房间数据到excel")
    @PostMapping("/excel")
    public ResponseResult<String> exportCommunityByIds(@RequestBody String[] ids, HttpServletResponse response) {

        System.out.println(ids);
        ArrayList<String> list = new ArrayList<>(Arrays.asList(ids));
        List<ZyRoom> zyRooms = roomService.listByIds(list);
        String fileName = UUID.randomUUID().toString();
        String path = "C:\\Users\\Administrator\\Desktop\\" + fileName + "房间信息绩表.xls";    //导出路径
        EasyExcelUtil.write(path, ZyRoom.class, zyRooms, 0, "房间信息绩表");
        return ResponseResult.ok("导出成功");

    }

    @ApiOperation("单删")
    @DeleteMapping("/delOne/{roomId}")
    public ResponseResult<String> delOne(@PathVariable String roomId) {
        roomService.removeById(roomId);
        return ResponseResult.ok("删除成功");
    }

    @ApiOperation("添加房间")
    @PostMapping("/add")
    public ResponseResult<String> addRoom(@RequestBody ZyRoom zyRoom) {
        roomService.save(zyRoom);
        return ResponseResult.ok("添加成功");
    }
    @ApiOperation("修改")
    @PostMapping("/update")
    public ResponseResult<String> update(@RequestBody ZyRoom zyRoom) {
        roomService.updateById(zyRoom);
        return ResponseResult.ok("修改成功");
    }

}
